import { fetchBaseQuery } from '@reduxjs/toolkit/query/react'
import { RootState } from '../store/types'

export const config = {
	apiUrl: 'https://api.react-learning.ru',
}

export const customBaseQuery = fetchBaseQuery({
	baseUrl: config.apiUrl,
	prepareHeaders: (headers, { getState }) => {
		const token = (getState() as RootState)?.user?.token
		if (token) {
			headers.set('Authorization', `Bearer ${token}`)
		}
		return headers
	},
})
